/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.wx.nrd.update.startup;

import cn.ac.iie.di.ban.ycl.wx.nrd.update.commons.RuntimeEnv;
import cn.ac.iie.di.ban.ycl.wx.nrd.update.commons.RuntimeEnvException;
import cn.ac.iie.di.ban.ycl.wx.nrd.update.exception.UpdateException;
import cn.ac.iie.di.ban.ycl.wx.nrd.update.util.MongoHelper;
import cn.ac.iie.di.ban.ycl.wx.nrd.update.util.SenderPool;
import cn.ac.iie.di.ban.ycl.wx.nrd.update.worker.Receiver;
import cn.ac.iie.di.datadock.rdata.exchange.client.core.REFieldType;
import cn.ac.iie.di.datadock.rdata.exchange.client.exception.REConnectionException;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.connection.REConnection;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSessionBuilder;

/**
 *
 * @author zwlin
 */
public class WxUpdateStartup {

	public static boolean isTest = false;

	private static SenderPool getSenderPool(String SendNamesrv, String SendTopic) throws REConnectionException {
		RESendSessionBuilder builder = (RESendSessionBuilder) new REConnection(SendNamesrv).getSendSessionBuilder(SendTopic);
		builder.addColumn("_id", REFieldType.String, true);    
                builder.addColumn("wx_key_fng", REFieldType.String, true);
		builder.addColumn("_key", REFieldType.String, true);
                builder.addColumn("_spec", REFieldType.String, true);
                builder.addColumn("_ch", REFieldType.Long, true);
                builder.addColumn("ut", REFieldType.Long, true);
		builder.addColumn("ct", REFieldType.Long, true);
		return new SenderPool(builder);
	}

	public static void main(String[] args) throws RuntimeEnvException, UpdateException, REConnectionException {
		RuntimeEnv.init();
		SenderPool[] pools = new SenderPool[2];
		pools[0] = getSenderPool(
				RuntimeEnv.getParamAsString(RuntimeEnv.RMQ_SEND1_NAMESRV),
				RuntimeEnv.getParamAsString(RuntimeEnv.RMQ_SEND1_TOPIC));
		pools[1] = getSenderPool(
				RuntimeEnv.getParamAsString(RuntimeEnv.RMQ_SEND2_NAMESRV),
				RuntimeEnv.getParamAsString(RuntimeEnv.RMQ_SEND2_TOPIC));
		MongoHelper[] helpers = new MongoHelper[2];
		helpers[0] = new MongoHelper(
				RuntimeEnv.getParamAsString(RuntimeEnv.MONGO1_HOSTS).split(";"),
				RuntimeEnv.getParamAsString(RuntimeEnv.MONGO1_DB),
				RuntimeEnv.getParamAsString(RuntimeEnv.MONGO1_TABLE),
				RuntimeEnv.getParamAsString(RuntimeEnv.MONGO1_USER),
				RuntimeEnv.getParamAsString(RuntimeEnv.MONGO1_PASSWD));
		helpers[1] = new MongoHelper(
				RuntimeEnv.getParamAsString(RuntimeEnv.MONGO2_HOSTS).split(";"),
				RuntimeEnv.getParamAsString(RuntimeEnv.MONGO2_DB),
				RuntimeEnv.getParamAsString(RuntimeEnv.MONGO2_TABLE),
				RuntimeEnv.getParamAsString(RuntimeEnv.MONGO2_USER),
				RuntimeEnv.getParamAsString(RuntimeEnv.MONGO2_PASSWD));
		Receiver receiver = new Receiver(
				RuntimeEnv.getParamAsString(RuntimeEnv.RMQ_RECEIVE_NAMESRV),
				RuntimeEnv.getParamAsString(RuntimeEnv.RMQ_RECEIVE_TOPIC),
				RuntimeEnv.getParamAsString(RuntimeEnv.RMQ_RECEIVE_GROUPNAME_PREFIX),
				pools,
				helpers);
		receiver.start();
	}
}
